package service.impl;

import bean.ClassBean;
import bean.CutPageBean;
import mapper.IClassMapper;
import org.apache.ibatis.session.SqlSession;
import service.IClassService;

import java.util.List;

public class ClassServiceImpl extends BaseService implements IClassService {

    @Override
    public List<ClassBean> findAll() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public boolean add(ClassBean clasz) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassBean> classList = mapper.findAll();
        for (int i = 0; i < classList.size(); i++) {
            if (classList.get(i).getName().equals(clasz.getName())) {
                return false;
            }
        }
        mapper.add(clasz);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public boolean delete(int classId) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        Integer studentNum = mapper.getStudentCount(classId);
        if (studentNum != 0) {
            return false;
        } else {
            mapper.delete(classId);
        }
        session.commit();
        session.close();
        return true;
    }

}
